require.config({
  baseUrl: "./script/",
  paths: {
    three: '../../lib/three',
    stats: '../../lib/stats.min',
    'three.OBJLoader': '../../lib/OBJLoader',
    'three.MTLLoader': '../../lib/MTLLoader'
  }
})

const requestAnimationFrame = window.requestAnimationFrame
  || window.mozRequestAnimationFrame
  || window.webkitRequestAnimationFrame
  || window.msRequestAnimationFrame
window.requestAnimationFrame = requestAnimationFrame;

require(['stats', 'three', 'three.OBJLoader', 'three.MTLLoader'], function (Stats, THREE) {

  let stat = null
  let renderer = null
  let scene = null
  let camera = null
  let id = null

  init()

  function init () {
    stat = new Stats();
    stat.domElement.style.position = 'absolute';
    stat.domElement.style.left = '';
    stat.domElement.style.right = '0px';
    stat.domElement.style.top = '0px';
    document.body.appendChild(stat.domElement);

    // 渲染器
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(400, 300);
    document.body.appendChild(renderer.domElement);
    renderer.setClearColor(0x000000);
    scene = new THREE.Scene();

    camera = new THREE.OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100);
    camera.position.set(5, 15, 25);
    camera.lookAt(new THREE.Vector3(0, 0, 0));
    scene.add(camera);

    const light = new THREE.AmbientLight(0xff0000)
    scene.add(light);

    const greenCube = new THREE.Mesh(new THREE.CubeGeometry(2, 2, 2),
      new THREE.MeshLambertMaterial({color: 0x00ff00}))
    greenCube.position.x = 3;
    scene.add(greenCube);

    const whiteCube = new THREE.Mesh(new THREE.CubeGeometry(2, 2, 2),
      new THREE.MeshLambertMaterial({color: 0xffffff}))
    whiteCube.position.x = -3;
    scene.add(whiteCube);

    id = requestAnimationFrame(draw);
  }

  function draw () {
    stat.begin();
    renderer.render(scene, camera);
    id = requestAnimationFrame(draw);
    stat.end();
  }

  function stop () {
    if (id !== null) {
      cancelAnimationFrame(id);
      id = null;
    }
  }

  function start () {
    if (id === null) {
      id = requestAnimationFrame(draw);
    }
  }

  Object.assign(window, {
    stop, start
  })

})
